package com.example.demo2.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo2.dao.JsonReader;
import com.example.demo2.entity.Passenger;
import com.example.demo2.framework.IOCMinimalistImitationVersion.Autowired;
import com.example.demo2.util.ChangeWord;
import com.example.demo2.util.Json2ObjectUtil;
import com.example.demo2.util.TransferPage;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

/**
 * @description This class is for user interaction between users and surname and id login page
 * @version 2.0
 */

public class SurnameController {
    @FXML
    private Button backButton;

    @FXML
    private TextField SurnameText;

    @FXML
    private TextField IdText;

    @FXML
    private Button LoginButton;
    static String target = "<TextArea fx:id=\"text1\" layoutX=\"225.0\" layoutY=\"106.0\" prefHeight=\"157.0\" prefWidth=\"635.0\" text=\"";
    static String end = "\">";


    @Autowired
    Json2ObjectUtil Passengers;

    public static Passenger m = null;


    public void backController(ActionEvent event) {
        Stage primaryStage = (Stage) backButton.getScene().getWindow();
        primaryStage.hide();
        TransferPage.transfer("passenger", "Passanger Login",600,1000);
    }

    public void loginController(ActionEvent event) {
        Stage primaryStage = (Stage) LoginButton.getScene().getWindow();
        primaryStage.hide();
        Passengers.Json2Object();

        for (int j = 0; j < Passengers.getPassenger().size(); j++) {
//            String BookingNum = passenger.getJSONObject(j).getString("BookingNum");
//            System.out.println(BookingNum);
            if (Passengers.getPassenger().get(j).getSurname().equals(SurnameText.getText())&&Passengers.getPassenger().get(j).getId().equals(IdText.getText())){
                m = Passengers.getPassenger().get(j);
            }

//            Passenger.BookingNum = BookingNumber.getText();
        }
        if (m != null) {
            String departure = m.getDeparture();
            String Date = m.getDate();
            String departureTime = m.getDepartureTime();
            String duration = m.getDuration();
            String arrivingTime = m.getArrivingTime();
            String theClass = m.getFlightClass();
            String destination = m.getDestination();
            String text = departure + "——————————————" + destination + "&#10;" +"         "+
                    Date + "&#10;" +"        "+
                    departureTime +"——"+arrivingTime+ "&#10;" +"          "+
                    duration + "&#10;" +"         "+
                    "Class: " + theClass;
            ChangeWord.changeText(target + text + end,"src/main/resources/fxml/flight-info.fxml",target + end);
            TransferPage.transfer("flight-info", "Flight Info", 600, 1000);
        } else {
            TransferPage.transfer("login-error", "Login Error", 300, 600);
        }
    }
}
